package net.wanho.systemSettings.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.systemSettings.dao.EmmPositionDAO;
import net.wanho.systemSettings.po.Position;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EmmPositionDAOImpl extends BaseDAOImpl<Position> implements EmmPositionDAO {
    @Override
    public boolean insert(Position position) throws DAOException {
        try {
            String sql = "insert\n" +
                    " into\n" +
                    " emm_position (POSITION_NAME,\n" +
                    " POSITION_LEVEL,\n" +
                    " CREATE_TIME,\n" +
                    " UPDATE_TIME)\n" +
                    "values(?,?,now(),now())";
            Object [] params = {position.getPositionName(),position.getPositionLevel()};
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean update(Position position) throws DAOException {
        try {
            String sql = "update\n" +
                    "emm_position\n" +
                    " set\n" +
                    " POSITION_NAME = ?,\n" +
                    " POSITION_LEVEL = ?,\n" +
                    " CREATE_TIME = now() ,\n" +
                    " UPDATE_TIME = now()\n" +
                    " where\n" +
                    " POSITION_ID = ?\n";
            Object [] params = {position.getPositionName(),position.getPositionLevel(),position.getPositionId()};
            return super.jdbcTemplate.updateBatch(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public Position findById(String id) throws DAOException {
        try {
            String sql = " select\n" +
                    " POSITION_ID positionId,\n" +
                    " POSITION_NAME positionName,\n" +
                    " POSITION_LEVEL positionLevel,\n" +
                    " CREATE_TIME createTime,\n" +
                    " UPDATE_TIME updateTime\n" +
                    "from\n" +
                    " emm_position where POSITION_ID = ? ";
            Object [] params = {id};
            return super.queryForObject(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Position> selectAll() throws DAOException {
        try {
            String sql = " select\n" +
                    " POSITION_ID positionId,\n" +
                    " POSITION_NAME positionName,\n" +
                    " POSITION_LEVEL positionLevel,\n" +
                    " CREATE_TIME createTime,\n" +
                    " UPDATE_TIME updateTime\n" +
                    "from\n" +
                    " emm_position";
            Object [] params = {};
            return super.queryForList(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Position> selectByCond(Map<String, String> condMap) throws DAOException {
        try {
            //基础SQL
            String sql = "SELECT\n" +
                    "  POSITION_ID       positionId,\n" +
                    "  POSITION_NAME     positionName,\n" +
                    "  POSITION_LEVEL    positionLevel,\n" +
                    "  CREATE_TIME       createTime,\n" +
                    "  UPDATE_TIME       updateTime\n" +
                    "FROM emm_position\n" +
                    "WHERE 1 = 1 ";
            //动态拼接SQL
            StringBuffer sb = new StringBuffer(sql);
            //条件无效  或者 没有条件
            boolean isCond = condMap == null || condMap.isEmpty();
            //创建集合 存放 动态参数值 ？ 替换的值
            List<Object> condParamValueList = new ArrayList<>();
            //有效条件
            if (!isCond) {
                condMap.forEach((k, v) -> {
                    sb.append(" AND ").append(k).append(" LIKE  ?");
                    condParamValueList.add("%" + v + "%");
                });
            }
            Object[] params = condParamValueList.toArray();
            return super.queryForList(sb.toString(), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }


    @Override
    public long count(String positionName) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    " COUNT(*)\n" +
                    " FROM emm_position\n" +
                    " WHERE POSITION_NAME = ?";
            Object[] params = {positionName};
            return super.jdbcTemplate.queryForLong(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean delete(String id) throws DAOException {
        try {
            String sql = "delete\n" +
                    " from\n" +
                    " emm_position\n" +
                    " where\n" +
                    " POSITION_ID = ? ";
            Object params [] = {id};
            return super.jdbcTemplate.delete(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
